from sqlalchemy import Column, Integer, String,ForeignKey
from sqlalchemy.orm import declarative_base
from pydantic import BaseModel

Base = declarative_base()

# 用户表模型
class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True, index=True)
    username = Column(String, unique=True)
    user_password = Column(String)
    pergrade = Column(Integer,ForeignKey("permission.pergrade"))  # 权限字段，只能是0，1，2
    note = Column(String)
    def __repr__(self):
        return (f"<User(username='{self.username}',note='{self.note}',pergrade='{self.pergrade}')>")

#用户信息表模型
class UserInfo(Base):
    __tablename__ = "userinfo"
    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey('users.id'))  # 外键字段
    username = Column(String, unique=True, index=True)
    email = Column(String)

    def __str__(self):
        return (f"<Userinfo(username='{self.username}',email='{self.email}',user_id='{self.user_id}')>")

# 权限表模型
class Permission(Base):
    __tablename__ = "permission"
    id = Column(Integer)
    pername = Column(String)
    pergrade = Column(Integer,primary_key=True, index=True)
    note = Column(String)

    # 定义权限的合法值
    VALID_PERMISSIONS = (0, 1, 2)
    def set_permission(self, pergrade):
        if pergrade in self.VALID_PERMISSIONS:
            self.permission = pergrade
        else:
            raise ValueError("Invalid permission value")
    def __repr__(self):
        return (f"<Permission(pername='{self.pername}',pergrade='{self.pergrade}'),note='{self.note}')>")

#菜单表模型
class Menu(Base):
    __tablename__ = "menu"
    id = Column(Integer, primary_key=True, index=True)
    menuname = Column(String)

    def __repr__(self):
        return (f"<Menu(menuname='{self.menuname}')>")

#定义操作表
class Operation(Base):
    __tablename__ = "operation"
    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey('users.id'))
    pergrade = Column(Integer,ForeignKey("permission.pergrade"))
    operation = Column(Integer,ForeignKey("menu.id"))
    note = Column(String)
    def __repr__(self):
        return (f"<Operation(user_id='{self.user_id}',pergrade='{self.pergrade}',operation='{self.operation}',note='{self.note}')>")


# 登录
class LoginUser(BaseModel):
    username: str
    password: str

# 注册模型
class RegisterUser(BaseModel):
    username: str
    password: str
    pergrade:int

#用户返回模型
class ReadUser(BaseModel):
    username: str
    email: str

#modbus数据模型
class ModbusData(Base):
    __tablename__ = 'modbusdata'

    id = Column(Integer, primary_key=True)
    slave_id = Column(Integer, nullable=False)
    value = Column(Integer, nullable=False)

    def __repr__(self):
        return f"<ModbusData(slave_id={self.slave_id}, value={self.value})>"

class ModbusExceptionData(Base):
    __tablename__ = 'modbus_exception_data'

    id = Column(Integer, primary_key=True)
    slave_id = Column(Integer, nullable=False)
    exception_code = Column(Integer, nullable=False)
    message = Column(String(255), nullable=True)
    timestamp = Column(Integer, nullable=False, default=lambda: int(round(time.time() * 1000)))

    def __repr__(self):
        return f"<ModbusExceptionData(slave_id={self.slave_id}, exception_code={self.exception_code}, message='{self.message}', timestamp={self.timestamp})>"